package com.team.base.module.controller.wxPay;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.team.base.common.utils.wxUtil.sdk.WXPayUtil;
import com.team.base.common.wxPay.Common;
import com.team.base.common.wxPay.RestUtil;
import com.team.base.module.entity.sys.order.Order;
import com.team.base.module.entity.sys.order.Project;
import com.team.base.module.service.sys.order.OrderService;
import com.team.base.module.service.sys.order.ProjectService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "小程序回调" })
@RequestMapping("/pay")
@RestController
public class PayNotifController {
	@Resource
	private OrderService orderService;

	@Resource
	private ProjectService projectService;

	@GetMapping("/notifyWx")
	@ApiOperation(value = "支付回调地址")
	public void notifyWx(HttpServletRequest request, HttpServletResponse response) {
		try {
			InputStream inStream = request.getInputStream();
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = inStream.read(buffer)) != -1) {
				outSteam.write(buffer, 0, len);
			}
			outSteam.close();
			inStream.close();
			String result = new String(outSteam.toByteArray(), "utf-8");
			Map<String, String> map = WXPayUtil.xmlToMap(result);
			String resultCode = map.get("result_code");
			String returnCode = map.get("return_code");
			String time_end = map.get("time_end");
			String out_trade_no = map.get("out_trade_no");
			String transaction_id = map.get("transaction_id");

			// 订单表
			Order order = new Order();
			if (("SUCCESS").equals(returnCode)) {// 通信成功
				if (("SUCCESS").equals(resultCode)) {// 交易成功

					Map<String, String> xml = new HashMap<>();
					xml.put("return_code", "SUCCESS");
					xml.put("return_msg", "OK");
					String signXml = WXPayUtil.generateSignedXml(xml, "");
					PrintWriter pw = response.getWriter();
					pw.println(signXml);
					pw.flush();

					SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
					Date date = format.parse(time_end);

					// 更改的订单信息
					order.setEndPayTime(date);
					order.setTrueCode(out_trade_no);
					order.setFinishTime(date);
					order.setPayType("0");
					order.setStatus("1");
					this.orderService.save(order);

					// 商品表
					order = this.orderService.get(order.getId());
					Project project = new Project();
					project.setProjectCode(order.getPayItem());
					project = this.projectService.get(project.getId());

					Map<String, String> paramMap = new HashMap<>();
					paramMap.put("return_code", "SUCCESS");
					paramMap.put("out_trade_no", order.getOrderNo());
					paramMap.put("transaction_id", transaction_id);
					paramMap.put("money", order.getMoney().toString());
					paramMap.put("pay_item", order.getPayItem());
					paramMap.put("order_no", out_trade_no);

					String param = Common.createLinkStringByGet(paramMap);
					RestUtil.load("project.getProjectUrl()", param);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
